import { FindOptions, MongoClient, ObjectId, UpdateFilter } from "mongodb";

const url = "mongodb://127.0.0.1:27017/";
const client = new MongoClient(url);

async function run() {
  try {
    await client.connect();
    const db = client.db("hello");
    const res = await db.command({ ping: 1 }); // 测试数据库是否成功
    console.log("connected", res);
    // 数据的插入
    const userCollection = db.collection<{ hobbies: string[] }>("user");
    /* let testArr = [];
    for (let i = 0; i <= 50000; i++) {
      testArr.push({
        type: "test",
        name: `name${i}`,
        age: 1,
      });
    }
    const result = await userCollection.insertMany(testArr);
    console.log(result); */

    // const result = await userCollection.find({ name: "name50000" }).explain();
    // const result = await userCollection.find({ id: new ObjectId("68cfaad1ec54e5d4473ad723") }).explain();

    // name字段创建索引id
    // const result = await userCollection.createIndex({ name: 1 });
    // console.log(result);

    // 查询当前文档有哪些索引id（在mongosh中成为getIndexs()）
    // const indexResult = await userCollection.listIndexes().toArray();
    // console.log(indexResult);

    // 删除索引id
    // const drop = await userCollection.dropIndex("name_1");
    // console.log("🚀 ~ run ~ drop:", drop);
    // console.log(result);
  } catch (error) {
    console.error("error", error);
  } finally {
    await client.close();
  }
}

run();
